package com.matt.controller.group.staffExtend;

import com.dotflat.rest.controller.RestfulBaseController;
import com.dotflat.staffExtend.domain.StaffExtendCareer;
import com.dotflat.staffExtend.domain.StaffExtendInfo;
import com.dotflat.staffExtend.po.GroupStaffExtendCareerPO;
import com.dotflat.staffExtend.po.StaffExtendCareerPO;
import com.dotflat.staffExtend.service.StaffExtendCareerService;
import com.dotflat.util.excelUtil.StringUtils;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.exception.BusinessException;
import com.matt.commons.model.StatusResponse;
import com.matt.commons.page.PagerQuery;
import com.matt.foundation.model.ErrorCode;
import com.matt.service.remoteCall.DTO.SsoAreaDTO;
import com.matt.service.remoteCall.SsoCallService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.matt.foundation.constant.RequestConstant.TICKET;

@RestController
@RequestMapping("/web/group/staff_extend/career")
@Slf4j
@Api(tags = "Web集团 - 员工扩展信息 - 履历 - 教育经历")
public class WebGroupStaffCareerController extends RestfulBaseController<StaffExtendCareer, StaffExtendCareerService, StaffExtendCareerPO> {

    @Autowired
    public void setBaseService(StaffExtendCareerService service){
        super.setBaseService(service);
    }

    @Autowired
    private StaffExtendCareerService careerService;

    @Autowired
    private SsoCallService ssoCallService;

    @ApiOperation(value = "获取所有老师的履历列表")
    @GetMapping("/list")
    @Override
    public StatusResponse getList(@RequestHeader(TICKET) String ticket, StaffExtendCareerPO param, PagerQuery pagerQuery){
        List<SsoAreaDTO> areaDTOList = ssoCallService.getGroupAllArea(ticket);
        List<String> ids = areaDTOList.stream().map(o->o.getId()).collect(Collectors.toList());

        Sort sort = Sort.by("created").descending();

        PageRequest pageRequest = PageRequest.of(pagerQuery.getPageNum(), pagerQuery.getPageSize(), sort);

        GroupStaffExtendCareerPO groupParam = GroupStaffExtendCareerPO.of(param, GroupStaffExtendCareerPO.class);
        if (StringUtils.isBlank(groupParam.getAreaId())){
            groupParam.setAreaId(null);
            groupParam.setAreaIds(ids);
        }else{
            Optional<SsoAreaDTO> anyArea = areaDTOList.stream().filter(o->o.getId().equals(groupParam.getAreaId())).findFirst();
            if (!anyArea.isPresent()){
                throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "指定园区不存在");
            }
        }

        Page<StaffExtendCareer> result = baseService.page(null, null, groupParam,  pageRequest);

        //补充来源
        //热修补
        result.getContent().stream().forEach(o->{
            StaffExtendInfo extendInfo = o.getStaffExtendInfo();
            o.setStaffId(extendInfo.getStaffId());
        });


        List<String> staffIds = result.getContent().stream().map(o->o.getStaffId()).collect(Collectors.toList());

        if (staffIds.size() > 0) {
            String[] staffIdsArr = new String[staffIds.size()];
            staffIdsArr = staffIds.toArray(staffIdsArr);
            List<StaffDTO> staffs = ssoCallService.getStaffDTOList(ticket, staffIdsArr);
            Map<String, StaffDTO> staffCache = staffs.stream().collect(Collectors.toMap(StaffDTO::getId, o->o));

            result.getContent().stream().forEach(o->{
                StaffDTO dto = staffCache.get(o.getStaffId());
                if (dto != null) {
                    o.setStaffName(dto.getRealName());
                }
            });
        }

        Map<String, SsoAreaDTO> areaCache = areaDTOList.stream().collect(Collectors.toMap(SsoAreaDTO::getId, o->o));
        if (areaCache.size() > 0 ){
            result.getContent().stream().forEach(o->{
                SsoAreaDTO dto = areaCache.get(o.getAreaId());
                if (dto != null) {
                    o.setAreaName(dto.getAreaName());
                }
            });
        }

        return StatusResponse.success(result);
    }


}
